import { useState, useEffect } from "react";
import {Form,Modal} from 'antd'
import useProcessDefOptionsHooks from './ProcessDefOptionsHooks.jsx'
import CamundaAPI from "@/api/CamundaApi.js";
function useStartProcess(props){
  
  const onClose = props.onClose ? props.onClose : () => {};
  const [form] = Form.useForm();

  useEffect(() => {
    // onProcessDefSearch("");

    if (props.procDefId) {
      form.setFieldsValue({
        procDefId: props.procDefId,
      });
    }

    form.setFieldsValue({
      processVar: "{\n" + '"name":"zhangsan",\n' + '"age":"5"\n' + "}",
    });
  }, []);

  const startProcess = () => {
    const params = form.getFieldsValue();
    
    if(params.processVar){
        params.processVar = JSON.parse(params.processVar)
    }
    console.log(params);
    CamundaAPI.startProcess(params).then((r) => {
      console.log(r);


      Modal.success({
        title:"Process Be Started Successful",
        content: <div>Process Instance Id : {r.result.processInstanceId} <br/>Business Key:{r.result.businessKey}</div>,
        centered: true,
        okText: "确认",
        width:700,
        onOk() {
          // 弹出登录弹框
        },
      });


    });
  };

  // ================ 流程定义下拉列表 ===================

  const {
    processDefList,
    onProcessDefSearch
  } = useProcessDefOptionsHooks();
  // const [processDefList, setProcessDefList] = useState([]);
  // const onProcessDefSearch = function (v) {
  //   const params = {};
  //   if (v) {
  //     params.keyWords = v;
  //   }
  //   CamundaAPI.queryProcessDefList(params).then((r) => {
  //     setProcessDefList(
  //       r.result.map((item) => {
  //         return {
  //           text: `${item.defName}(${item.defKey}[Ver.${item.defVersion}])`,
  //           value: item.procDefId,
  //         };
  //       })
  //     );
  //   });
  // };

  

  return {
    onClose,
    startProcess,
    onProcessDefSearch,
    processDefList,
    form
  }
}

export default useStartProcess;